/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dao.exceptions.NonexistentEntityException;
import dao.exceptions.RollbackFailureException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
import pojo.Orderdetail;
import pojo.Userorder;
import pojo.Extraprice;
import pojo.Food;

/**
 *
 * @author A7med Kotb
 */
public class OrderdetailJpaController implements Serializable {

    public OrderdetailJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Orderdetail orderdetail) throws RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Userorder userOrderId = orderdetail.getUserOrderId();
            if (userOrderId != null) {
                userOrderId = em.getReference(userOrderId.getClass(), userOrderId.getId());
                orderdetail.setUserOrderId(userOrderId);
            }
            Extraprice extraPriceId = orderdetail.getExtraPriceId();
            if (extraPriceId != null) {
                extraPriceId = em.getReference(extraPriceId.getClass(), extraPriceId.getId());
                orderdetail.setExtraPriceId(extraPriceId);
            }
            Food foodId = orderdetail.getFoodId();
            if (foodId != null) {
                foodId = em.getReference(foodId.getClass(), foodId.getId());
                orderdetail.setFoodId(foodId);
            }
            em.persist(orderdetail);
            if (userOrderId != null) {
                userOrderId.getOrderdetailCollection().add(orderdetail);
                userOrderId = em.merge(userOrderId);
            }
            if (extraPriceId != null) {
                extraPriceId.getOrderdetailCollection().add(orderdetail);
                extraPriceId = em.merge(extraPriceId);
            }
            if (foodId != null) {
                foodId.getOrderdetailCollection().add(orderdetail);
                foodId = em.merge(foodId);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Orderdetail orderdetail) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Orderdetail persistentOrderdetail = em.find(Orderdetail.class, orderdetail.getId());
            Userorder userOrderIdOld = persistentOrderdetail.getUserOrderId();
            Userorder userOrderIdNew = orderdetail.getUserOrderId();
            Extraprice extraPriceIdOld = persistentOrderdetail.getExtraPriceId();
            Extraprice extraPriceIdNew = orderdetail.getExtraPriceId();
            Food foodIdOld = persistentOrderdetail.getFoodId();
            Food foodIdNew = orderdetail.getFoodId();
            if (userOrderIdNew != null) {
                userOrderIdNew = em.getReference(userOrderIdNew.getClass(), userOrderIdNew.getId());
                orderdetail.setUserOrderId(userOrderIdNew);
            }
            if (extraPriceIdNew != null) {
                extraPriceIdNew = em.getReference(extraPriceIdNew.getClass(), extraPriceIdNew.getId());
                orderdetail.setExtraPriceId(extraPriceIdNew);
            }
            if (foodIdNew != null) {
                foodIdNew = em.getReference(foodIdNew.getClass(), foodIdNew.getId());
                orderdetail.setFoodId(foodIdNew);
            }
            orderdetail = em.merge(orderdetail);
            if (userOrderIdOld != null && !userOrderIdOld.equals(userOrderIdNew)) {
                userOrderIdOld.getOrderdetailCollection().remove(orderdetail);
                userOrderIdOld = em.merge(userOrderIdOld);
            }
            if (userOrderIdNew != null && !userOrderIdNew.equals(userOrderIdOld)) {
                userOrderIdNew.getOrderdetailCollection().add(orderdetail);
                userOrderIdNew = em.merge(userOrderIdNew);
            }
            if (extraPriceIdOld != null && !extraPriceIdOld.equals(extraPriceIdNew)) {
                extraPriceIdOld.getOrderdetailCollection().remove(orderdetail);
                extraPriceIdOld = em.merge(extraPriceIdOld);
            }
            if (extraPriceIdNew != null && !extraPriceIdNew.equals(extraPriceIdOld)) {
                extraPriceIdNew.getOrderdetailCollection().add(orderdetail);
                extraPriceIdNew = em.merge(extraPriceIdNew);
            }
            if (foodIdOld != null && !foodIdOld.equals(foodIdNew)) {
                foodIdOld.getOrderdetailCollection().remove(orderdetail);
                foodIdOld = em.merge(foodIdOld);
            }
            if (foodIdNew != null && !foodIdNew.equals(foodIdOld)) {
                foodIdNew.getOrderdetailCollection().add(orderdetail);
                foodIdNew = em.merge(foodIdNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = orderdetail.getId();
                if (findOrderdetail(id) == null) {
                    throw new NonexistentEntityException("The orderdetail with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Orderdetail orderdetail;
            try {
                orderdetail = em.getReference(Orderdetail.class, id);
                orderdetail.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The orderdetail with id " + id + " no longer exists.", enfe);
            }
            Userorder userOrderId = orderdetail.getUserOrderId();
            if (userOrderId != null) {
                userOrderId.getOrderdetailCollection().remove(orderdetail);
                userOrderId = em.merge(userOrderId);
            }
            Extraprice extraPriceId = orderdetail.getExtraPriceId();
            if (extraPriceId != null) {
                extraPriceId.getOrderdetailCollection().remove(orderdetail);
                extraPriceId = em.merge(extraPriceId);
            }
            Food foodId = orderdetail.getFoodId();
            if (foodId != null) {
                foodId.getOrderdetailCollection().remove(orderdetail);
                foodId = em.merge(foodId);
            }
            em.remove(orderdetail);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Orderdetail> findOrderdetailEntities() {
        return findOrderdetailEntities(true, -1, -1);
    }

    public List<Orderdetail> findOrderdetailEntities(int maxResults, int firstResult) {
        return findOrderdetailEntities(false, maxResults, firstResult);
    }

    private List<Orderdetail> findOrderdetailEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Orderdetail.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Orderdetail findOrderdetail(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Orderdetail.class, id);
        } finally {
            em.close();
        }
    }

    public int getOrderdetailCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Orderdetail> rt = cq.from(Orderdetail.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
